//-----------------------------------------------------------------------
// <copyright file="GiaoDichDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using APPro.Entities;

using Pyco;
using Pyco.Diagnostics;
using Pyco.Data;

namespace APPro.Data
{    
    public abstract class GiaoDichDAOBase
    {
        #region Common methods
        public virtual GiaoDich CreateGiaoDichFromReader(IDataReader reader)
        {
            GiaoDich item = new GiaoDich();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("Mobile"))) item.Mobile = (string)reader["Mobile"];
                if (!reader.IsDBNull(reader.GetOrdinal("NgayGD"))) item.NgayGD = (DateTime)reader["NgayGD"];
                if (!reader.IsDBNull(reader.GetOrdinal("GioGD"))) item.GioGD = (DateTime)reader["GioGD"];
                if (!reader.IsDBNull(reader.GetOrdinal("SoDiemTruoc"))) item.SoDiemTruoc = (int)reader["SoDiemTruoc"];
                if (!reader.IsDBNull(reader.GetOrdinal("SoDiemGD"))) item.SoDiemGD = (int)reader["SoDiemGD"];
                if (!reader.IsDBNull(reader.GetOrdinal("SoDiemSau"))) item.SoDiemSau = (int)reader["SoDiemSau"];
                if (!reader.IsDBNull(reader.GetOrdinal("NoiDung"))) item.NoiDung = (string)reader["NoiDung"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateGiaoDichFromReaderException, ex);
            }
            return item;
        }
        #endregion
		
        #region CreateGiaoDich methods
            
        public virtual void CreateGiaoDich(GiaoDich giaoDich)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGiaoDichInsert");
                
				database.AddInParameter(dbCommand, "@Mobile", DbType.String, giaoDich.Mobile);
				database.AddInParameter(dbCommand, "@NgayGD", DbType.DateTime, giaoDich.NgayGD);
				database.AddInParameter(dbCommand, "@GioGD", DbType.DateTime, giaoDich.GioGD);
				database.AddInParameter(dbCommand, "@SoDiemTruoc", DbType.Int32, giaoDich.SoDiemTruoc);
				database.AddInParameter(dbCommand, "@SoDiemGD", DbType.Int32, giaoDich.SoDiemGD);
				database.AddInParameter(dbCommand, "@SoDiemSau", DbType.Int32, giaoDich.SoDiemSau);
				database.AddInParameter(dbCommand, "@NoiDung", DbType.String, giaoDich.NoiDung);
				
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateGiaoDichException, ex);
            }
        }

        #endregion
		
        #region UpdateGiaoDich methods
        
        public virtual void UpdateGiaoDich(GiaoDich giaoDich)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGiaoDichUpdate");            
                
                database.AddInParameter(dbCommand, "@Mobile", DbType.String, giaoDich.Mobile);
                database.AddInParameter(dbCommand, "@NgayGD", DbType.DateTime, giaoDich.NgayGD);
                database.AddInParameter(dbCommand, "@GioGD", DbType.DateTime, giaoDich.GioGD);
                database.AddInParameter(dbCommand, "@SoDiemTruoc", DbType.Int32, giaoDich.SoDiemTruoc);
                database.AddInParameter(dbCommand, "@SoDiemGD", DbType.Int32, giaoDich.SoDiemGD);
                database.AddInParameter(dbCommand, "@SoDiemSau", DbType.Int32, giaoDich.SoDiemSau);
                database.AddInParameter(dbCommand, "@NoiDung", DbType.String, giaoDich.NoiDung);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessUpdateGiaoDichException, ex);
            }
        }
        
        #endregion
		
        #region DeleteGiaoDich methods
        public virtual void DeleteGiaoDich(string mobile, DateTime ngayGD, DateTime gioGD)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGiaoDichDelete");
                
                database.AddInParameter(dbCommand, "@Mobile", DbType.String, mobile);
                database.AddInParameter(dbCommand, "@NgayGD", DbType.DateTime, ngayGD);
                database.AddInParameter(dbCommand, "@GioGD", DbType.DateTime, gioGD);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessDeleteGiaoDichException, ex);
            }
        }

        #endregion
		
        #region GetGiaoDich methods
        
        public virtual GiaoDich GetGiaoDich(string mobile, DateTime ngayGD, DateTime gioGD)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGiaoDichGet");
                
                database.AddInParameter(dbCommand, "@Mobile", DbType.String, mobile);
                database.AddInParameter(dbCommand, "@NgayGD", DbType.DateTime, ngayGD);
                database.AddInParameter(dbCommand, "@GioGD", DbType.DateTime, gioGD);
                
                GiaoDich giaoDich =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        giaoDich = CreateGiaoDichFromReader(reader);
                        reader.Close();
                    }
                }
                return giaoDich;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetGiaoDichException, ex);
            }
        }
        
        #endregion
		
        #region GetGiaoDichList methods
        public virtual GiaoDichCollection GetGiaoDichList(GiaoDichColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGiaoDichGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                GiaoDichCollection giaoDichCollection = new GiaoDichCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        GiaoDich giaoDich = CreateGiaoDichFromReader(reader);
                        giaoDichCollection.Add(giaoDich);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return giaoDichCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetGiaoDichListException, ex);
            }
        }
        
        public virtual GiaoDichCollection GetGiaoDichList(GiaoDichColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetGiaoDichList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
    }
}